﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Collections;
using System.IO;

namespace CapabilitiesDetector
{
    public partial class InputUA : System.Web.UI.Page
    {
        internal static  Panel CreateResultLabel(string capabilityKey, string value)
        {
          
            Label lblKey = new Label();
            lblKey.Width = Unit.Percentage(50);
            lblKey.Text = String.Format("{0}: ", capabilityKey);
            Label lblCapValue = new Label();
            lblCapValue.Width = Unit.Percentage(50);
            lblCapValue.Text = String.Format(" {0} </br>", value);
            Panel rowPnl = new Panel();
            rowPnl.Controls.Add(lblKey);
            rowPnl.Controls.Add(lblCapValue);   
            return   rowPnl ;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Load Default Current Devices User Agent
                TextBoxUA.Text = Request.UserAgent;
            }
            DisplayBrowserCapabilities();
        }

        private void DisplayBrowserCapabilities()
        {


            PanelBasic.Controls.Add(CreateResultLabel("Count", Context.Request.Browser.Capabilities.Count.ToString()));
            PanelAdvanced.Controls.Add(CreateResultLabel("Count", ((IDictionary)Context.Request.Browser.Capabilities["WurflCapabilities"]).Count.ToString()));
   
               
                foreach (DictionaryEntry key in Context.Request.Browser.Capabilities)
                {
                    PanelBasic.Controls.Add(CreateResultLabel(key.Key.ToString(), (key.Value == null) ? string.Empty : key.Value.ToString()));
                }

                foreach (DictionaryEntry key in (IDictionary)Context.Request.Browser.Capabilities["WurflCapabilities"])
                {
                    PanelAdvanced.Controls.Add(CreateResultLabel(key.Key.ToString(), (key.Value == null) ? string.Empty : key.Value.ToString()));
                }
                PanelBasic.Visible = true;
                PanelAdvanced.Visible = true;
            
        }

       
       
        protected void btnDispCaps_Click(object sender, EventArgs e)
        {
            panelUA.Visible = false; 
            PanelBasic.Visible = false;
            PanelAdvanced.Visible = false;

            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped) + ResolveUrl("~/DispCapabilities.aspx"));
            myHttpWebRequest.UserAgent = TextBoxUA.Text;
            // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            // Display the contents of the page to the console.
            Stream streamResponse = myHttpWebResponse.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            Char[] readBuff = new Char[256];
            int count = streamRead.Read(readBuff, 0, 256);
            Response.Write("\nThe capabilities of User Agent " + TextBoxUA.Text + " are :\n");
            while (count > 0)
            {
                String outputData = new String(readBuff, 0, count);
                Response.Write(outputData);
                count = streamRead.Read(readBuff, 0, 256);
            }
            // Release the response object resources.
            streamRead.Close();
            streamResponse.Close();
            myHttpWebResponse.Close();
        }
    }
}
